package com.lly.songhigh.greendao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.lly.songhigh.application.BaseApplication;
import com.lly.songhigh.entity.dao.AddTo;
import com.lly.songhigh.entity.dao.AddToDao;
import com.lly.songhigh.entity.dao.Collection;
import com.lly.songhigh.entity.dao.CollectionDao;
import com.lly.songhigh.entity.dao.DaoMaster;
import com.lly.songhigh.entity.dao.DaoSession;
import com.lly.songhigh.entity.dao.RecentPlay;
import com.lly.songhigh.entity.dao.RecentPlayDao;
import com.lly.songhigh.entity.dao.RecentSearch;
import com.lly.songhigh.entity.dao.RecentSearchDao;

import java.util.List;

/**
 * Created by 李新伟 on 15/11/6.
 */
public class DaoSingleton {
    private volatile static DaoSingleton instance;
    private static final String DATABASE_NAME = "songhigh.db";

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private AddToDao addToDao;
    private CollectionDao collectionDao;
    private RecentPlayDao recentPlayDao;
    private RecentSearchDao recentSearchDao;

    private DaoSingleton() {
        context = BaseApplication.getContext();
    }

    public static DaoSingleton getInstance() {
        if (instance == null) {
            synchronized (DaoSingleton.class) {
                if (instance == null) {
                    instance = new DaoSingleton();
                }
            }
        }
        return instance;
    }

    public DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }
        return helper;
    }

    public SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    public DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public AddToDao getAddToDao() {
        if (addToDao == null) {
            addToDao = getDaoSession().getAddToDao();
        }
        return addToDao;
    }

    public RecentSearchDao getRecentSearchDao() {
        if (recentSearchDao == null) {
            recentSearchDao = getDaoSession().getRecentSearchDao();
        }
        return recentSearchDao;
    }

    public CollectionDao getCollectionDao() {
        if (collectionDao == null) {
            collectionDao = getDaoSession().getCollectionDao();
        }
        return collectionDao;
    }

    public RecentPlayDao getRecentPlayDao() {
        if (recentPlayDao == null) {
            recentPlayDao = getDaoSession().getRecentPlayDao();
        }
        return recentPlayDao;
    }

    public List<RecentSearch> getRecentSearch() {
        return getRecentSearchDao().loadAll();
    }

    public List<RecentSearch> getRecentSearchByTag(String tag){
        return getRecentSearchDao().queryBuilder().where(RecentSearchDao.Properties.SearchTv.eq(tag)).list();
    }

    public List<AddTo> getAddTo() {
        return getAddToDao().loadAll();
    }

    public List<RecentPlay> getRecentPlay() {
        return getRecentPlayDao().loadAll();
    }

    public List<Collection> getCollection() {
        return getCollectionDao().loadAll();
    }

    public List<AddTo> getAddToById(int id) {
        return getAddToDao().queryBuilder().where(AddToDao.Properties.SongListId.eq(id)).list();
    }

    public List<Collection> getCollectionById(int id) {
        return getCollectionDao().queryBuilder().where(CollectionDao.Properties.SongId.eq(id)).list();
    }

    public List<RecentPlay> getRecentPlayById(int id) {
        return getRecentPlayDao().queryBuilder().where(RecentPlayDao.Properties.SongId.eq(id)).list();
    }

    public void deleteAddToById(int id) {
        getAddToDao().queryBuilder().where(AddToDao.Properties.SongListId.eq(id)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public void deleteCollectionById(int id) {
        getCollectionDao().queryBuilder().where(CollectionDao.Properties.SongId.eq(id)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public void deleteRecentPlayById(int id) {
        getRecentPlayDao().queryBuilder().where(RecentPlayDao.Properties.SongId.eq(id)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public void deleteRecentSearchByName(String name) {
        getRecentSearchDao().queryBuilder().where(RecentSearchDao.Properties.SearchTv.eq(name)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public void deleteRecentSearchAll() {
        getRecentSearchDao().deleteAll();
    }
}
